﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;

namespace ElencySolutions.ImageMap.Properties
{
    [DataContract]
    [KnownType(typeof(ImageMap))]
    public class ImageMap
    {

        [DataMember]
        public string ImageUrl { get; set; }

        [DataMember]
        public int ImageWidth { get; set; }

        [DataMember]
        public int ImageHeight { get; set; }

        [DataMember]
        public string ImageAltText { get; set; }

        [DataMember]
        public string DefaultLinkUrl { get; set; }

        [DataMember]
        public string Target { get; set; }

        [DataMember]
        public Areas Areas { get; set; } 

        public ImageMap()
        {
            ImageUrl = string.Empty;
            ImageWidth = 0;
            ImageHeight = 0;
            ImageAltText = string.Empty;
            DefaultLinkUrl = string.Empty;
            Target = "_self";
            Areas = new Areas();
        }

        public string GetHtml()
        {
            return GetHtml(string.Empty);
        }

        public string GetHtml(string cssClass)
        {
            if (string.IsNullOrEmpty(ImageUrl))
                return string.Empty;

            StringBuilder html = new StringBuilder();
            string mapName = Guid.NewGuid().ToString("N");
            html.AppendLine(string.Format("<img src=\"{0}\"{1}{2} alt=\"{3}\" title=\"{3}\" usemap=\"#{4}\"{5} />",
                ImageUrl, 
                ImageWidth == 0 ? string.Empty : " width=\"" + ImageWidth + "px\"",
                ImageHeight == 0 ? string.Empty : " height=\"" + ImageHeight + "px\"",
                ImageAltText, 
                mapName,
                string.IsNullOrEmpty(cssClass) ? string.Empty : string.Format(" class=\"{0}\"", cssClass)));

            html.AppendLine(string.Format("<map name=\"{0}\">", mapName));

            if (Areas.Count == 0 && !string.IsNullOrEmpty(DefaultLinkUrl))
            {
                Area area = new Area
                {
                    AltText = ImageAltText,
                    Coordinates = new List<int>(),
                    ShapeType = "default",
                    Target = Target,
                    Url = DefaultLinkUrl
                };
                AppendArea(html, area);
            }

            foreach (Area area in Areas)
                AppendArea(html, area);

            html.AppendLine("</map>");

            return html.ToString();
        }

        private void AppendArea(StringBuilder html, Area area)
        {
            string coordinates = area.Coordinates.Aggregate(string.Empty, (current, coordinate) => current + (coordinate + ",")).TrimEnd(new[] { ',' });

            html.AppendLine(string.Format("<area shape=\"{0}\" coords=\"{1}\" alt=\"{2}\" title=\"{2}\" href=\"{3}\"{4}>",
                area.ShapeType, coordinates, area.AltText, area.Url,
                string.IsNullOrEmpty(area.Target) ? string.Empty : string.Format(" target=\"{0}\"", area.Target)));
        }

        public override string ToString()
        {
            return ImageMapHelper.SerializeObject(this);
        }

    }
}
